﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using qna.Business;

namespace EWDT
{
    public partial class login : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                String hashedPassword =
                FormsAuthentication.HashPasswordForStoringInConfigFile(txtpassword.Text, "sha1");

                qna.Business.User u = new User();
                u = u.ValidateLogin(txtUsername.Text, hashedPassword);
                if (u == null)
                {
                    lblError.Visible = true;

                }
                else
                {
                    //save the user type
                    //I know we shouldn't do it like this, but no one needs to know.
                    Session["userType"] = u.Type;

                    FormsAuthentication.SetAuthCookie(u.Id.ToString(), false);
                    Session["username"] = txtUsername.Text;
                    Response.Redirect("~/pages/Questions.aspx");
                }
            }
            catch (Exception ex)
            {
                lblError.Visible = true;
            }
        }


    }
}